/**
 * <p>Title: ServiceZP16.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-8 上午07:51:18  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zp.service;

import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.iplat4j.es.domain.ESUser;
import com.baosight.iplat4j.security.bridge.SecurityBridge;
import com.baosight.iplat4j.security.bridge.SecurityBridgeFactory;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zp.ZPUtils;
import com.baosight.sczx.zp.domain.ZPResourceset;
import com.baosight.sczx.zp.domain.ZPUser;
import com.baosight.sczx.zp.domain.ZPUserset;

/**
 * 角色管理——人员列表
 * <p>Title: ServiceZP16.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-8 上午07:51:18  张志芳  Create
 *</pre>
 */
public class ServiceZP16 extends ServiceEPBase {
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:25:10 张志芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo){
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:25:20 张志芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo)
	{		
		//获得角色主键
		String businessUnitPost = inInfo.getString("roleId");	
		
		//构造返回值outInfo
		EiInfo outInfo = new EiInfo();
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		outInfo.setAttr(inInfo.getAttr());
		EiBlock resultBlock = outInfo.addBlock(EiConstant.resultBlock);
		resultBlock.setBlockMeta(new ESUser().eiMetadata);

		//提取查询条件
		QueryMap queryMap = QueryMap.getQueryMap(inInfo);
		
		//通过epass提供的类查询某角色下的资源组
		SecurityBridge b = SecurityBridgeFactory.getBridge();
		List members = b.getRoleMembers(businessUnitPost, "", "", queryMap.getOffset(), queryMap.getLimit());
		resultBlock.setRows(members);
		
		int totalnum = b.getRoleMemberCount(businessUnitPost, "", "");
		queryMap.setCount(totalnum);
		queryMap.setEiInfo(outInfo, EiConstant.resultBlock);
		return outInfo;
	}
	
	/**
	 * 添加用户前的查询
	 * <p>Function: queryRes</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:25:29 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryUser(EiInfo inInfo)
	{
		//去除已经添加过的人员
		String roleId = inInfo.getString("roleId");
		if(roleId!=null)
		{
			StaticTools.setQueryParameters(inInfo, "roleId", roleId, true);
		}
		inInfo.addBlock("userResult").set("showCount", "true");
		inInfo.addBlock("userResult").set("limit", "-1");
		
		if(inInfo.getString("loginName") != null && !"".equals(inInfo.getString("loginName")))
		{
			StaticTools.setQueryParameters(inInfo, "loginName", inInfo.getString("loginName"), true);
		}
		if(inInfo.getString("displayName") != null && !"".equals(inInfo.getString("displayName")))
		{
			StaticTools.setQueryParameters(inInfo, "displayName", inInfo.getString("displayName"), true);
		}
		
		EiInfo outInfo = super.query(inInfo, "ZPUser.queryUser", new ESUser(), false, null, EiConstant.queryBlock, "userResult", "userResult");
		return outInfo;
	}
	
	/**
	 * 添加用户
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:26:49 张志芳
	 * </p>
	 *
	 */
	public EiInfo add(EiInfo inInfo)
	{
		String roleLabel = inInfo.getString("roleId");
		List members = inInfo.getBlock("result").getRows();
		for (int i = 0; i < members.size(); i++) {
			Map member = (Map) members.get(i);
			String userName = (String) member.get("loginName");
			SecurityBridgeFactory.getBridge().addUserToRole(roleLabel, userName);
		}
		return inInfo;
	}
	
	/**
	 * 删除用户
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:27:04 张志芳
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo)
	{
		String roleId = inInfo.getString("roleId");
		List members = inInfo.getBlock("result").getRows();
		for (int i = 0; i < members.size(); i++) {
			Map member = (Map) members.get(i);
			String userName = (String) member.get("loginName");
			SecurityBridgeFactory.getBridge().removeUserFromRole(roleId, userName);
		}
		return query(inInfo);
	}
}
